package com.beyond.action;

import java.util.List;
import java.util.Map;

import com.beyond.dao.DataQueryDao;
import com.beyond.entity.TableConstruct;
import com.opensymphony.xwork2.ActionSupport;

public class VerifyDataAction extends ActionSupport {
	private static final long serialVersionUID = 1L;

	private List<TableConstruct> list ;
	private Map<String,String> map ;
	private String tableName ;
	private String Sql ;
	private TableConstruct tabObj;
	private String actionName;
	private String etl_tab_count;
	private String errer_list ;
	private String sumSql ;
	
	@Override
	public String execute() throws Exception {
		DataQueryDao queryDataDao = new DataQueryDao();
//		tableName="f_ps_gds05_2g_cell_15m";
		// ETL汇聚：15M:H:D 记录数
		
		tableName = tableName.replaceAll("I", "F");
		
		etl_tab_count = getEtlTabCount(tableName,queryDataDao);
		
		// 错误话单表（条数：I表：F表：汇聚表）
		errer_list = getErrorSessionList(queryDataDao);
		
		// 
		map = queryDataDao.getColumnName(tableName);
		
		// 维度字段
		String dimention = map.get("dimention"); 
		// 指标字段
		String target = map.get("target"); 
		
		// 求和sql
		sumSql = getSumSql(target,target,tableName);
		
		// 查询维度字段
		Sql = createDimetionSql(dimention,dimention,tableName,"").toLowerCase();
		
//		String countSql = "SELECT "+this.createCountSql(dimention)+","+this.createCountSql(target)+" FROM "+tableName ;
//		queryDataDao.getCountSql(countSql);
		
//		System.out.println(Sql);
		
		return SUCCESS;
	}
	
	// ETL汇聚，统计各表的记录数
	public String getEtlTabCount(String tabName,DataQueryDao queryDataDao){
		String pre_tab = "";
		String tab_h = "",tab_d = "",tab_15m = "";
		String rsTab = "";
		
		if(tabName != null && !"".equals(tabName)){
			pre_tab = tabName.substring(0,tabName.lastIndexOf("_")+1);
			String sub_tab = tableName.substring(tabName.lastIndexOf("_")+1,tabName.length()).toLowerCase();
			if("15m".equals(sub_tab)){
				tab_15m = tabName;
				tab_h = pre_tab+"H";
				tab_d = pre_tab+"D";
			}else if("h".equals(sub_tab)){
				tab_h = tabName;
				tab_d = pre_tab+"D";
			}
		}
		
		if(!"".equals(tab_15m)){
			rsTab = tab_15m+":"+tab_h+":"+tab_d+"=";
			rsTab += queryDataDao.getEtlTabCount(tab_15m)+":"
							+queryDataDao.getEtlTabCount(tab_h)+":"
							+queryDataDao.getEtlTabCount(tab_d);
		}else if(!"".equals(tab_h)){
			rsTab =tab_h+":"+tab_d+"=";
			rsTab += queryDataDao.getEtlTabCount(tab_h)+":"
							+queryDataDao.getEtlTabCount(tab_d);
		}
		
		return rsTab;
	}
	
	// 错误话单
	public String getErrorSessionList(DataQueryDao queryDataDao){
		String rs = "";
		String alarm_tab = "F_PS_ERR_CDR_ALARM_D"; 
		String gns_tab = "I_PS_GDS01_CDR"; 
		String userivce_tab = "I_PS_GDS02_CDR"; 
		String wap_tab = "I_PS_GDS03_CDR"; 
		String mt_tab = "I_PS_GDS04_CDR"; 
		String dns_tab = "I_PS_GDS06_CDR"; 
		int alarm_count = queryDataDao.getEtlTabCount(alarm_tab);
		rs = "[F:GNS"+"="+alarm_count+":"+queryDataDao.getEtlTabCount(gns_tab)+"] \n" +
				" [F:UserService"+"="+alarm_count+":"+queryDataDao.getEtlTabCount(userivce_tab)+"] \n"+
				" [F:Wap&Http"+"="+alarm_count+":"+queryDataDao.getEtlTabCount(wap_tab)+"] \n"+
				" [F:MMS_MT"+"="+alarm_count+":"+queryDataDao.getEtlTabCount(mt_tab)+"] \n"+
				" [F:DNS"+"="+alarm_count+":"+queryDataDao.getEtlTabCount(dns_tab)+"] \n"
				;
		return rs;
	}
	
	// 查询维度sql
	public String createDimetionSql(String dimention,String target,String tbName,String tag){
		String sql = "SELECT "+ addPrefixTarget(dimention,tag) +" from "+tbName+" t,"+tbName+" a " +
				" where "+addTargetSql(target);
		
		return sql;
	}
	
	// 求和sql
	public String getSumSql(String dimention,String target,String tbName){
		String sql = "SELECT "+ addPrefixTarget(dimention,"sum") +" from "+tbName+" t " +
		" where ";

		return sql;
	}
	
	// 字段添加标示
	public String addPrefixTarget(String strs,String flag){
		StringBuilder sb = new StringBuilder();
		if(strs != null && !"".equals(strs)){
			String[] strArray = strs.split(",");
			for (String str : strArray) {
				if("sum".equals(flag))
					sb.append(" SUM(t.").append(str).append("),");
				else if("hour_id".equals(str.toLowerCase()) || "minute_15_id".equals(str.toLowerCase()))
					sb.append(" distinct t.").append(str).append(",");
				else
					sb.append(" t.").append(str).append(",");
			}
		}
		
		return sb.substring(0,sb.length()-1);
	}
	
	// Where判断条件
	public String addTargetSql(String strs){
		StringBuilder sb = new StringBuilder();
		if(strs != null && !"".equals(strs)){
			String[] strArray = strs.split(",");
			for (String str : strArray) {
				sb.append(" t.").append(str).append(" = ").append(" a.").append(str).append(" and");
			}
		}
		
		return sb.substring(0,sb.length()-3);
	}
	
	// 组装维度或指标为-1的sql
	public String createCountSql(String sql){
		StringBuilder sbs = new StringBuilder();
		String[] dimSql = sql.split(",");
		for (String dim : dimSql) {
			// SELECT COUNT((CASE terminal_oid WHEN -1 THEN -1 WHEN 0 THEN 1 END ))
			sbs.append("COUNT ((CASE ").append(dim).append(" WHEN -1 THEN -1 WHEN 0 THEN 1 END))").append(dim).append(",");
		}
		
		return sbs.substring(0,sbs.length()-1);
	}
	
	public String test() throws Exception {
		System.out.println("test");
		return SUCCESS;
	}


	public List<TableConstruct> getList() {
		return list;
	}

	public void setList(List<TableConstruct> list) {
		this.list = list;
	}

	public TableConstruct getTabObj() {
		return tabObj;
	}

	public void setTabObj(TableConstruct tabObj) {
		this.tabObj = tabObj;
	}

	public String getTableName() {
		return tableName;
	}

	public void setTableName(String tableName) {
		this.tableName = tableName;
	}

	public String getSql() {
		return Sql;
	}

	public void setSql(String sql) {
		Sql = sql;
	}
	
	public String getEtl_tab_count() {
		return etl_tab_count;
	}

	public void setEtl_tab_count(String etlTabCount) {
		etl_tab_count = etlTabCount;
	}
	public String getErrer_list() {
		return errer_list;
	}

	public void setErrer_list(String errerList) {
		errer_list = errerList;
	}

	public String getSumSql() {
		return sumSql;
	}

	public void setSumSql(String sumSql) {
		this.sumSql = sumSql;
	}
}
